<?php

	if(!isset($_SESSION['username'])){
			echo "<script>alert('请登录！');window.location.href='index.php?m=admin&c=manage'</script>";die;
		}


	//权限管理
	authonity($con);

	$id = isset($_GET['id'])?$_GET['id']:1;
	//权限角色列表查询
	$permission = dbselect($con,'permission');

	assign('permission',$permission);

	//查询当前将修改的数据
	$filed = 'username,nicheng,email,phone,per_id,status';

	$upd_data = dbselOne($con,'yonghu',$filed,$id);

	$upd_data_per = dbselect($con,'permission','*',"id='{$upd_data["per_id"]}'");
	// var_dump($upd_data_per);die;
	$upd_per = isset($upd_data_per[0])?$upd_data_per[0]:0;

	assign('upd_data',$upd_data);

	assign('upd_per',$upd_per);



	if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])=='xmlhttprequest'){

		if(isset($_POST['bnicheng'])){

			$nicheng = $_POST['bnicheng'];

			$nuniqueness = dbselect($con,'yonghu','nicheng',"nicheng='{$nicheng}'");

			if($nuniqueness){
				echo 1;die;
			}else{
				echo 0;die;
			}

		}else if(isset($_POST['bpassword'])){

			$bpassword = $_POST['bpassword'];

			$passrule = "/^[\w_]{6,10}$/";

			echo preg_match($passrule, $bpassword);die;	


		}else if(isset($_POST['phone'])){
			$phone = $_POST['phone'];

			$phonerule = "/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/";

			$puniqueness = dbselect($con,'yonghu','phone',"phone='{$phone}'");


			if($puniqueness){

				$pun = 1 ;
			}else{
				$pun = 0 ;
			}

			

			$list['prule'] = preg_match($phonerule,$phone);

			$list['puniqueness'] = $pun;	

			echo json_encode($list);die;

		}else if(isset($_POST['email'])){
			$email = $_POST['email'];

			$emailrule = "/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/";

			$euniqueness = dbselect($con,'yonghu','email',"email='{$email}'");


			if($euniqueness){

				$eun = 1 ;
			}else{
				$eun = 0 ;
			}

			

			$list['erule'] = preg_match($emailrule,$email);

			$list['euniqueness'] = $eun;	

			echo json_encode($list);die;

		}else if(isset($_POST['submit'])){
			if(!empty($_POST['password'])){
				$data['password'] = $_POST['spassword'];
			}
			if(!empty($_POST['sphone'])){
				$data['phone'] = $_POST['sphone'];
			}
			if(!empty($_POST['semail'])){
				$data['email'] = $_POST['semail'];
			}

			$data['per_id'] = $_POST['spermission'];

			$data['status'] = $_POST['status'];
			$upd_admin = dbupdate($con,'yonghu',$data,"id='{$id}'");

			if($upd_admin){
				echo 1;die;
			}else{
				echo 0;die;
			}
		}
	}



?>